import 'package:flutter/material.dart';
import 'package:state_management_flutter_bloc/search_configuration.dart';
import 'package:state_management_flutter_bloc/themes/themes.dart';

class AdvancedSearchPage extends StatefulWidget {
  final SearchConfiguration searchConfiguration;

  const AdvancedSearchPage({Key key, this.searchConfiguration})
      : super(key: key);

  @override
  _AdvancedSearchPageState createState() => _AdvancedSearchPageState();
}

class _AdvancedSearchPageState extends State<AdvancedSearchPage> {
  void _onPositionSelected(bool isSelected,String position){
    if(isSelected){
      widget.searchConfiguration.selectedPositions.add(position);
    }else{
      widget.searchConfiguration.selectedPositions.remove(position);
    }
  }
  void _onLeagueSelected(bool isSelected,String leagueKey){
    if(isSelected){
      widget.searchConfiguration.selectedLeagues.putIfAbsent(leagueKey,()=>widget.searchConfiguration.leagues[leagueKey]);
    }else{
      widget.searchConfiguration.selectedLeagues.remove(leagueKey);
    }
  }

  void _onNationSelected(bool isSelected,String nationCode){
    if(isSelected){
      widget.searchConfiguration.selectedNations.putIfAbsent(nationCode,()=>widget.searchConfiguration.nations[nationCode]);
    }else{
      widget.searchConfiguration.selectedNations.remove(nationCode);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          title: Text('高级查询'),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 24.0),
                  child: Text(
                    "POSITIONS",
                    style: titleStyle.copyWith(fontWeight: FontWeight.w800),
                  ),
                ),
                Padding(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
                  child: Wrap(
                    spacing: 15,
                    children: <Widget>[
                      for (var position in widget.searchConfiguration.positions)
                        ChoiceChip(
                          onSelected: (isSelected){
                            setState(() {
                              _onPositionSelected(isSelected,position);
                            });
                          },
                            selected: widget
                                .searchConfiguration.selectedPositions
                                .contains(position),
                            selectedColor: Colors.blueGrey,
                            backgroundColor:  Colors.grey.shade300,
                            label: Text(position),
                            labelPadding:
                                const EdgeInsets.symmetric(horizontal: 16),
                            labelStyle: widget
                                    .searchConfiguration.selectedPositions
                                    .contains(position)
                                ? choicesTextStyle.copyWith(color: Colors.white)
                                : choicesTextStyle),
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 24.0),
                  child: Text('LEAGUES',style: titleStyle.copyWith(fontWeight: FontWeight.w800),),
                ),
                Padding(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
                  child: Wrap(
                    spacing: 10,
                    children: <Widget>[
                      for (var leagueKey
                          in widget.searchConfiguration.leagues.keys)
                        ChoiceChip(
                          onSelected: (isSelected){
                            setState(() {
                              _onLeagueSelected(isSelected,leagueKey);
                            });
                          },
                          selected: widget
                              .searchConfiguration.selectedLeagues.keys
                              .contains(leagueKey),
                          selectedColor: Colors.blueGrey,
                          backgroundColor:  Colors.grey.shade300,
                          pressElevation: 10,
                          label: Text(leagueKey),
                          labelPadding:
                              const EdgeInsets.symmetric(horizontal: 16),
                          labelStyle: widget
                                  .searchConfiguration.selectedLeagues.keys
                                  .contains(leagueKey)
                              ? choicesTextStyle.copyWith(color: Colors.white)
                              : choicesTextStyle,
                        )
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 24.0),
                  child: Text('NATIONS',style: titleStyle.copyWith(fontWeight: FontWeight.w800),),
                ),
                Padding(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
                  child: Wrap(
                    spacing: 10,
                    children: <Widget>[
                      for (var nationKey
                          in widget.searchConfiguration.nations.keys)
                        ChoiceChip(
                          onSelected: (isSelected){
                            setState(() {
                              _onNationSelected(isSelected,nationKey);
                            });
                          },
                          selected: widget
                              .searchConfiguration.selectedNations.keys
                              .contains(nationKey),
                          selectedColor: Colors.blueGrey,
                          backgroundColor: Colors.grey.shade300,
                          pressElevation: 10,
                          label: Text(nationKey),
                          labelPadding:
                              const EdgeInsets.symmetric(horizontal: 16),
                          labelStyle: widget
                                  .searchConfiguration.selectedNations.keys
                                  .contains(nationKey)
                              ? choicesTextStyle.copyWith(color: Colors.white)
                              : choicesTextStyle,
                        )
                    ],
                  ),
                ),
                Center(
                  child: FlatButton(onPressed: (){
                    Navigator.of(context).pop({"search_configuration":widget.searchConfiguration});
                  }, child: Text('查询',style: advanceSearchTextStyle,),color: Colors.blue[900],),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
